#include "pixmapbutton.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QBitmap>

PixmapButton::PixmapButton(QWidget *parent) : QPushButton(parent),
            m_pushbutton_status(BC::PixmapButtonNoStatus), m_mouse_pressed(false)
{
    m_pixmap_released = nullptr;
    m_pixmap_pressed = nullptr;
    m_pixmap_background = nullptr;
}

PixmapButton::PixmapButton(const QString &released, const QString &pressed,
            const int width, const int height, QWidget *parent)
            : QPushButton(parent), m_pushbutton_status(BC::PixmapButtonDoubleStatus), m_mouse_pressed(false)
{
    m_pixmap_released = new QPixmap(released);
    m_pixmap_pressed = new QPixmap(pressed);
    m_pixmap_background = nullptr;
    if (width == 0 || height == 0) {
        m_pixmap_pressed->scaled(m_pixmap_released->size());
    }
    else {
        m_pixmap_released->scaled(width, height);
        m_pixmap_pressed->scaled(width, height);
    }
    this->resize(m_pixmap_released->size());
}

PixmapButton::PixmapButton(const QPixmap &released, const QPixmap &pressed, QWidget *parent)
            : QPushButton(parent), m_pushbutton_status(BC::PixmapButtonDoubleStatus), m_mouse_pressed(false)
{
    m_pixmap_released = new QPixmap(released);
    m_pixmap_pressed = new QPixmap(pressed);
    m_pixmap_background = nullptr;
    this->resize(m_pixmap_released->size());
}

PixmapButton::PixmapButton(const QString &released, const QString &pressed, const QString &background,
            const int width, const int height, QWidget *parent)
            : QPushButton(parent), m_pushbutton_status(BC::PixmapButtonDoubleStatus), m_mouse_pressed(false)
{
    m_pixmap_released = new QPixmap;
    if (!m_pixmap_released->load(released)) {
        qDebug() << "PixmapButton load released fail";
    }
    m_pixmap_pressed = new QPixmap;
    if (!m_pixmap_pressed->load(pressed)) {
        qDebug() << "PixmapButton load pressed fail";
    }
    m_pixmap_background = new QPixmap;
    if (!m_pixmap_background->load(background)) {
        qDebug() << "PixmapButton load background fail";
    }
    if (width == 0 || height == 0) {
        m_pixmap_released->scaled(m_pixmap_background->size());
        m_pixmap_pressed->scaled(m_pixmap_background->size());
    }
    else {
        m_pixmap_released->scaled(width, height);
        m_pixmap_pressed->scaled(width, height);
        m_pixmap_background->scaled(width, height);
    }
    this->resize(m_pixmap_background->size());
    this->QWidget::setMask(QBitmap(m_pixmap_background->mask()));
}

PixmapButton::~PixmapButton()
{
    if (m_pixmap_background) {
        delete m_pixmap_background;
    }
    if (m_pixmap_released) {
        delete m_pixmap_released;
    }
    if (m_pixmap_pressed) {
        delete m_pixmap_pressed;
    }
}

void PixmapButton::paintEvent(QPaintEvent *event)
{
    if (m_pushbutton_status == BC::PixmapButtonNoStatus) {
        QPushButton::paintEvent(event);
    }
    else if (m_pushbutton_status == BC::PixmapButtonSignStatus) {
        QPainter painter(this);
        painter.drawPixmap(0, 0, *m_pixmap_released);
    }
    else {
        QPainter painter(this);
        if (m_mouse_pressed) {
            painter.drawPixmap(0, 0, *m_pixmap_pressed);
        }
        else {
            painter.drawPixmap(0, 0, *m_pixmap_released);
        }
    }
}

void PixmapButton::mousePressEvent(QMouseEvent *event)
{
    if (m_pushbutton_status == BC::PixmapButtonNoStatus) {
        QPushButton::mousePressEvent(event);
    }
    else if(m_pushbutton_status == BC::PixmapButtonSignStatus) {
        QPushButton::mousePressEvent(event);;
    }
    else {
        if (event->button() != Qt::LeftButton) {
            return;
        }
        m_mouse_pressed = true;
        this->update();
    }
}

void PixmapButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_pushbutton_status == BC::PixmapButtonNoStatus) {
        QPushButton::mouseReleaseEvent(event);
    }
    else if (m_pushbutton_status == BC::PixmapButtonSignStatus) {
        QPushButton::mouseReleaseEvent(event);
    }
    else {
        if (event->button() != Qt::LeftButton) {
            return;
        }
        m_mouse_pressed = false;
        this->update();
        if (event->x() < m_pixmap_released->size().width() && event->y() < m_pixmap_released->size().height()) {
            this->clicked();
        }
    }
}

